<?php
error_reporting(-1);
ini_set('display_errors', 1);

date_default_timezone_set('Asia/Ho_Chi_Minh');

require APPPATH . "libraries/simple_html_dom.php";

class admin_news_auto extends CI_Controller {

    protected $_page_id = 'ul_postcontainer';
    protected $_page_name = "admin_news_auto";
    protected $_page_news_edit = "admin_news_auto_edit";
    protected $data; //data for this page.
    protected $_page_title = ADMINTITLE_NEWS_AUTO;
    protected $_base_url;
    protected $folder_save_news_image = "style/upimg_man/image/news/";
    protected $news_get_count;
    protected $returnCode;
    protected $news_lastest_url;
    protected $flag_check_lastest_news;
    protected $flag_check_update;

    function __construct() {
        $this->_initdata();
    }

    function index() {
        $this->load_news_auto();
        $this->showadmin($this->data);
        //$this->get_news_auto();
    }

    function load_news_auto() {
        $iCurPage = $this->uri->segment(2);
        if ($iCurPage < 1) {
            $iCurPage = 1;
        }
        @$limit = PAGESIZE;
        $start = $iCurPage * PAGESIZE - PAGESIZE;
        if (!($start > 0)) {
            $start = 0;
        }

        if (!$limit > 0) {
            $limit = 20;
        }
        list($this->data['newsCount'], $this->data['aNews']) = $this->_get_news_from_db($start, $limit);
        $iRow_Count = $this->data['newsCount'];
        $this->data['paging'] = $this->paging($iCurPage, ceil($iRow_Count / PAGESIZE), $this->config->item('base_url') . $this->_page_name, $iRow_Count);
        $this->data['cur_page'] = $iCurPage;
        $this->data['previous_id'] = 1;
    }

    //local func
    function paging($page, $totalpage, $pagelink, $totalrows) {
        $paging_str = '';
        $paging_str .= 'Trang: &nbsp;&nbsp;&nbsp;';
        if ($page > 1)
            $paging_str .= '<img src="' . $this->config->item('base_url') . 'style/img/img21_arrow_left.gif" align="absmiddle" style="cursor:pointer;" onclick="location.href=' . "'" . $pagelink . '/' . ($page - 1) . "'" . '">';
        else
            $paging_str .= '<img src="' . $this->config->item('base_url') . 'style/img/img21_arrow_left.gif" align="absmiddle">';
        $paging_str .= '&nbsp;&nbsp;&nbsp;';

        // list 10 pagelink
        $startpage = 1;
        if ($page > 5)
            $startpage = $page - 4;
        $endpage = $totalpage;
        if ($totalpage - $page > 5)
            $endpage = $page + 5;
        for ($i = $startpage; $i < $page; $i++)
            $paging_str .= ' <a href="' . $pagelink . '/' . $i . '" class="link">' . $i . '</a>';
        $paging_str .= ' <a href="' . $pagelink . '/' . $i . '" class="link_selected">' . $page . '</a>';
        for ($i = $page + 1; $i <= $endpage; $i++)
            $paging_str .= ' <a href="' . $pagelink . '/' . $i . '" class="link">' . $i . '</a>';

        $paging_str .= '&nbsp;&nbsp;&nbsp;';
        if ($page < $totalpage)
            $paging_str .= '<img src="' . $this->config->item('base_url') . 'style/img/img22_arrow_right.gif" align="absmiddle" style="cursor:pointer;" onclick="location.href=' . "'" . $pagelink . '/' . ($page + 1) . "'" . '">';
        else
            $paging_str .= '<img src="' . $this->config->item('base_url') . 'style/img/img22_arrow_right.gif" align="absmiddle">';
        $paging_str .= '&nbsp;&nbsp;&nbsp;</span></td>';

        $paging_str .= '&nbsp;&nbsp;&nbsp;<span class="paging_total"><strong>[ </strong>T&#7893;ng c&#7897;ng: <font color="Red">' . $totalrows . '</font> k&#7871;t qu&#7843; <strong> ]</strong></span>';
        return $paging_str;
    }

    function _initdata() {
        parent::__construct();
        session_start();
		set_time_limit(0);
        $this->load->model('Adminmodel');
        $this->load->helper('url');
        $this->_check_admin_login();

        //init data (this page, header admin)

        $this->data['img_url'] = $this->config->item('img_url') . $this->folder_save_news_image;
        $this->data['static_url'] = $this->config->item('img_url');
        $this->data['base_url'] = $this->config->item('base_url');
        $this->data['page_title'] = $this->_page_title;
        $this->data['admintitle_news'] = ADMINTITLE_NEWS;
        $this->data['admintitle_news_auto'] = ADMINTITLE_NEWS_AUTO;
        $this->data['admintitle_video'] = ADMINTITLE_VIDEO;
        $this->data['admintitle_customs'] = ADMINTITLE_CUSTOMS;
        $this->data['admintitle_adv'] = ADMINTITLE_ADV;
        $this->data['admintitle_faq'] = ADMINTITLE_FAQ;
        $this->data['admintitle_contact'] = ADMINTITLE_CONTACT;
        $this->data['admintitle_common'] = ADMINTITLE_COMMON;
        $this->data['admintitle_stats'] = ADMINTITLE_STATS;
        $this->data['admintitle_beuser'] = ADMINTITLE_BEUSER;
        $this->data['website_name'] = WEBSITE_NAME;
        $this->data['link_page_name'] = $this->_page_name;
        $this->data['is_addnew'] = ""; //show link add
        $this->data['is_addlist'] = "NOT_SHOW"; //show link list
        $this->data['is_news_auto'] = "AUTO"; //show button get news auto
        $this->data['page_id'] = $this->_page_id;
        $this->data['page_name'] = $this->_page_name;
        $this->news_get_count = 0;
    }

    function _check_admin_login() {
        if (($this->Adminmodel->_check_login(SESSION_ADMINUSERNAME) == false) ||
                (time() - $this->Adminmodel->_get_session(SESSION_ADMINTIMEOUT)) > SESSION_ADMINTIMEOUTVALUE) {
            $base_url = $this->config->item('base_url');
            redirect($base_url . 'admin_login', 'location', 301);
        } else
            $this->Adminmodel->_set_session(SESSION_ADMINTIMEOUT, time());
    }

    //Hiển thi tin tức từ DB
    function _get_news_from_db($page_index, $page_size) {
        return $this->Adminmodel->_get_news_auto_from_db($page_index, $page_size);
    }

    function get_news_auto() {
        $arrNewsUrl = array(
            //"http://www.baohaiquan.vn/pages/hai-quan.aspx",
            //"http://www.baohaiquan.vn/pages/an-ninh-xuat-nhap-khau.aspx",
            "http://baocongthuong.com.vn/thuong-mai"
        );
        
        $arrOrginalNews = array(1);//0 bao hai quan, 1: bao cong thuong, 2: vietrade ... bao khac
        
        $numberOfNewsUrl = sizeof($arrNewsUrl);
        $result = 0;
        if ($_SERVER["REQUEST_METHOD"] === "POST") {
            $tablename = "news_auto";
            
            for($i=0; $i<$numberOfNewsUrl; $i++){
                $pageIndex = 1;
                $this->flag_check_update = 0;
                
                $tempUrlLastest = $this->Adminmodel->_get_news_url_lastest($arrNewsUrl[$i]);
                $this->news_lastest_url = sizeof($tempUrlLastest) == 0 ? "INSERT" : $tempUrlLastest[0]->url_lastest;
                
                $data_news = NULL;
                $itemPerPage = 0;
                
                while(true){
                    if($arrOrginalNews[$i] == 0){
                        $fullUrl = $arrNewsUrl[$i] . "?P=" . $pageIndex;
                        $itemPerPage = 8;
                        $data_news = $this->get_news_auto_from_bao_hai_quan($fullUrl, $arrNewsUrl[$i]);
                    }
                    else if($arrOrginalNews[$i] == 1){
                        $fullUrl = $arrNewsUrl[$i] . "&BRSR=" . ($pageIndex-1)*20;
                        $itemPerPage = 11;
                        $data_news = $this->get_news_auto_from_bao_cong_thuong($fullUrl, $arrNewsUrl[$i]);
                    }
                    else if($arrOrginalNews[$i] == 2){
                        $startPage = ($pageIndex-1) * 10;
                        $fullUrl = $arrNewsUrl[$i] . "?start=" . $startPage;
                        $itemPerPage = 10;
                        $data_news = $this->get_news_auto_from_vietrade($fullUrl, $arrNewsUrl[$i]);
                    }

                    $stt = 0;
                    $insert_data = array();
                    foreach ($data_news as $item) {
                        $insert_data[$stt]["title"] = $item["title"];
                        $insert_data[$stt]["desc"] = $item["desc"];
                        $insert_data[$stt]["content"] = $item["content"];
                        $insert_data[$stt]["img"] = $item["image"];
                        $insert_data[$stt]["pubdate"] = $item["pubdate"];
                        $insert_data[$stt]["news_orginal"] = $item["orginal"];
                        $insert_data[$stt]["news_key"] = $item["key"];
                        $insert_data[$stt]["url"] = $item["url"];

                        $stt++;
                    }
                    if (sizeof($insert_data) > 0) {
                        $result = $this->Adminmodel->_insert_news_to_db($tablename, $insert_data);
                    }
                    
                    if(sizeof($data_news) < $itemPerPage){
                        break;
                    }
					
                    $pageIndex++;

                    if($pageIndex == 2){
                        break;
                    }
                }
            }
        }
        echo $this->news_get_count;
    }

    function get_news_auto_from_bao_hai_quan($fullUrl, $orginalNews) {

        //dinh nghia html can lay
        $item_tag = "div[id=TinHaiQuan_Main] div[class=lvkd-item]";
        $title_tag = "div[class=title-item]";
        $desc_tag = "div[class=content-item]";
        $news_url_tag = "a|0";
        $news_content = "div[class=article-ds]";
        $url_add_image = "http://www.baohaiquan.vn";
        $url_add_news_detail = "http://www.baohaiquan.vn";
        $number_get_item = -1;

        $data_temp = $this->_get_news_from_html_dom($fullUrl, $item_tag, $title_tag, $desc_tag, $news_url_tag, $orginalNews, $news_content, $this->folder_save_news_image, $url_add_image, $url_add_news_detail, $number_get_item, "Báo Hải Quan");

        return $data_temp;
    }
    
    function get_news_auto_from_bao_cong_thuong($fullUrl, $orginalNews){
        //dinh nghia html can lay
        $item_tag = "div[class=content-wrap] section[class=cate_content] section[class=cate_content] article";
        $title_tag = "header[class=header] a";
        $desc_tag = "header[class=header] p[class=summary]";
        $news_url_tag = "a|0";
        $news_content = "div[id=content] article";
        $url_add_image = "";
        $url_add_news_detail = "";
        $number_get_item = 20;

        $data_temp = $this->_get_news_from_html_dom($fullUrl, $item_tag, $title_tag, $desc_tag, $news_url_tag, $orginalNews, $news_content, $this->folder_save_news_image, $url_add_image, $url_add_news_detail, $number_get_item, "Báo Công Thương");

        return $data_temp;
        
    }
    
    function get_news_auto_from_vietrade($fullUrl, $orginalNews){
        $item_tag = "div[class=content_inner] div";
        $title_tag = "a[class=contentpagetitle]";
        $desc_tag = "table[class=contentpaneopen] p";
        $news_url_tag = "a|0";
        $news_content = "div[class=content_inner] table[class=contentpaneopen] p";
        $url_add_image = "http://www.vietrade.gov.vn";
        $url_add_news_detail = "http://www.vietrade.gov.vn";
        $number_get_item = 10;

        $data_temp = $this->_get_news_from_html_dom($fullUrl, $item_tag, $title_tag, $desc_tag, $news_url_tag, $orginalNews, $news_content, $this->folder_save_news_image, $url_add_image, $url_add_news_detail, $number_get_item, "Vietrade");

        return $data_temp;
		
    }

    //Lấy thông tin tin tức từ HTML DOM
    function _get_news_from_html_dom($html_url, $item_tag, $title_tag, $desc_tag, $news_url_tag, $news_key, $content_tag, $url_folder_save_news_image, $url_add_image, $url_add_news_detail, $number_get_item, $news_orginal) {
        $html = file_get_html($html_url);
        $items = $html->find($item_tag);

        $data = array();
        $stt = 0;

        foreach ($items as $item) {
            //define variable
            $news_url = "";

            //get news detail url
            $arr_news_url = explode("|", $news_url_tag);
            if (sizeof($arr_news_url) == 2) {
                $url = $item->find($arr_news_url[0], (int) $arr_news_url[1])->href;
            } else {
                $url = $item->find($arr_news_url[0])->href;
            }
            $news_url .= $url_add_news_detail . $url;
            
            //compare url lastest
            if (strcmp($this->news_lastest_url, "INSERT") == 0) {
                //$this->Adminmodel->_insert_url_lastest($news_url, $news_key);
                $this->news_lastest_url = "";
            } 
            else if ($this->news_lastest_url != "") {
                if (strcmp($this->news_lastest_url, $news_url) == 0) {
                    break;
                } else if ($this->flag_check_update == 0) {
                    $this->Adminmodel->_update_url_lastest_by_original_news($news_url, $news_key);
                    $this->flag_check_update = 1;
                }
            }

            //get content of news
            $content_temp = $this->_get_news_content_from_url($news_url, $content_tag, $url_add_image, $url_folder_save_news_image);
            if($content_temp == NULL){
                continue;
            }
            
            $content_news = base64_encode($content_temp["content"]);
            $image_show = $content_temp["image"]; //image show on page list news
            //get title of news
            $arrTile = explode("+", $title_tag);
            $title = "";
            for ($i = 0; $i < sizeof($arrTile); $i++) {
                $title_get = $item->find($arrTile[$i]);
                if (sizeof($title_get) > 0) {
                    $title = $title_get[0]->plaintext;
                    break;
                }
            }
            
            //get short desc of news
            $short_desc_get = $item->find($desc_tag);
            $short_desc = $short_desc_get[0]->plaintext;

            //get publish date of news
            //$pub_date_get = $item->find($pubdate_tag);
            $pub_date = ""; //$pub_date_get[0]->plaintext;

            $data[$stt]["url"] = $news_url;
            $data[$stt]["content"] = $content_news;
            $data[$stt]["image"] = $image_show;
            $data[$stt]["title"] = $title;
            $data[$stt]["desc"] = $short_desc;
            $data[$stt]["pubdate"] = $pub_date;
            $data[$stt]["key"] = $news_key;
            $data[$stt]["orginal"] = $news_orginal;
            $stt++;
            $this->news_get_count++;
            
            if ($number_get_item == -1) {
                continue;
            } else if ($stt == $number_get_item) {
                break;
            }
        }
        
        return $data;
    }

    //Lấy thông tin tin tức tự động từ rss
    function _get_news_from_rss($rss_url, $item_tag, $title_tag, $desc_tag, $pubdate_tag, $news_url_tag, $news_original, $content_tag, $url_folder_save_news_image, $url_add_image, $url_add_news_detail, $number_get_item) {

        $dom = new DOMDocument();
        $dom->load($rss_url);
        $items = $dom->getElementsByTagName($item_tag);
        $data = array();
        $stt = 0;

        foreach ($items as $item) {
            $date = new DateTime();
            $date_format = $date->format("YmdHis");

            $url = $url_add_news_detail . $item->getElementsByTagName($news_url_tag)->item(0)->nodeValue;
            if ($url == $url_lastest) {
                break;
            } else if ($stt == 0) {

                //update $url_lastest
            }
            $content_img = $this->_get_news_content_from_url($url, $content_tag);
            $filename = "";
            if ($content_img["image"] != "") {
                $url_img = $url_add_image . str_replace(" ", "%20", utf8_encode(strtolower($content_img["image"])));
                $filename = $date_format . "_" . basename($content_img["image"]);
                $this->saveImageToLocal($url_img, $url_folder_save_news_image . $filename);
            }

            $data[$stt]["url"] = $url;
            $data[$stt]["content"] = $content_img["content"];
            $data[$stt]["image"] = $filename;
            $data[$stt]["title"] = $item->getElementsByTagName($title_tag)->item(0)->nodeValue;
            $data[$stt]["desc"] = $item->getElementsByTagName($desc_tag)->item(0)->nodeValue;
            $data[$stt]["pubdate"] = $item->getElementsByTagName($pubdate_tag)->item(0)->nodeValue;
            $data[$stt]["original"] = $news_original;
            $stt++;
            $this->news_get_count++;

            if ($number_get_item == -1) {
                continue;
            } else if ($stt == $number_get_item) {
                break;
            }
        }

        return $data;
    }

    //Lấy nội dung của tin tức từ url
    function _get_news_content_from_url($url, $content_tag, $url_add_image, $url_folder_save_news_image) {

        $result = array();
        $result["content"] = "";
        $result["image"] = "";
        $html = @file_get_html(strtolower($url));
        if($html === FALSE){
            return NULL;
        }
        
        $news_content = $html->find($content_tag);
        echo $news_content . "\n";
        $content = $news_content[0];

        //Save all image to local
        $all_image = $content->find("img");
        $number_of_image = sizeof($all_image); //get number of image in html page
        for ($i = 0; $i < $number_of_image; $i++) {
            $date = new DateTime();
            $date_format = $date->format("YmdHis");

            $full_url_image = $url_add_image . $all_image[$i]->src;
            $url_img_decode = str_replace(" ", "%20", utf8_encode(strtolower($full_url_image)));
            //$url_img_decode = str_replace(" ", "%20", $full_url_image);
            $filename = $date_format . "_" . basename($full_url_image);
            $link_save_image_url = $url_folder_save_news_image . $filename;
            $this->saveImageToLocal($url_img_decode, $link_save_image_url);

            //thiet lap lai ten file image tro vao local
            $new_full_image_url = $this->data['img_url'] . $filename;
            $content = str_replace($all_image[$i]->src, $new_full_image_url, $content);
            

            if ($i == 0) {
                $result["image"] = $filename; //image show at page default
            }
        }

        $result["content"] = $content;
        return $result;
    }

    //Lưu hình ảnh lấy được vào local host
    function saveImageToLocal($url, $save_location) {
        $data = @file_get_contents($url);
        if($data == ''){
            return "";
        }
        $savefile = fopen($save_location, 'w');
        fwrite($savefile, $data);
        fclose($savefile);
        return filesize($save_location);
    }

    //edit news from db news auto
    function news_edit() {

        if (!empty($_POST['action']) && $_POST['action'] == "news_edit") {
            //get submit data
            $img_save = "upimg_man/image/news/";

            $this->submitdata['cate_id'] = $_POST['cate_id'];
            $this->submitdata['parent_cate_url'] = $this->Adminmodel->_get_url_of_parent_cate($_POST['cate_id']); //get url của cate cha của cate news hiện hành.
            $this->submitdata['title'] = $_POST['title'];
            $this->submitdata['url'] = convertToVn(trim($_POST['title']));
            $this->submitdata['short_desc'] = $_POST['shortDesc'];
            $this->submitdata['full_desc'] = $_POST['fullDesc']; 
            $this->submitdata['create_user'] = $this->Adminmodel->_get_session(SESSION_ADMINUSERNAME);
            $this->submitdata['create_date'] = date("Y-m-d H:i:s");
            $this->submitdata['online_date'] = date("Y-m-d H:i:s");
            $this->submitdata['tag'] = $_POST['tag'];
            $this->submitdata['is_hot'] = $_POST['isHot'];
            $this->submitdata['display_date'] = $_POST['displayDate'];
            $this->submitdata['status'] = $_POST['status'];
            $this->submitdata['img'] = $img_save . $_POST['imageFeed'];
            $this->submitdata['img_thumb'] = $img_save . $_POST['imageFeed'];
            $this->submitdata['img_thumb2'] = $img_save . $_POST['imageFeed'];
            $this->submitdata['img_thumb3'] = $img_save . $_POST['imageFeed'];
            $this->submitdata['source'] = $_POST['newsSource'];

            //insert news
            $insert_id = $this->Adminmodel->_insert_news('news', $this->submitdata);
            if($insert_id > 0){
                $this->Adminmodel->_update_status_news_auto($_POST['news_id'], 1);
            }
            echo $insert_id;
        } else {
            $idNews = $this->uri->segment(3);
            $data_news = $this->Adminmodel->_get_news_auto_by_id($idNews);

            //return view
            $this->data['page_name'] = $this->_page_news_edit;
            $this->data['is_news_auto'] = "";
            if (sizeof($data_news) == 1) {
                $this->data['aNewsCate'] = $this->ddl_get_news_cate_list();
                $title_temp = explode("&nbsp", $data_news[0]->title, 2);
                $this->data['newsTitle'] =  $title_temp[0];
                
                $this->data['newsImage'] = $data_news[0]->img;
                $this->data['newsShortDesc'] = str_replace("(HQ Online)-", "" , $data_news[0]->desc);
                $this->data['newsFullDesc'] = str_replace("(HQ Online)-", "" , base64_decode($data_news[0]->content));
                $this->data['newsOrginal'] = $data_news[0]->url;
                $this->data['newsId'] = $idNews;
                $this->data['isNewsExists'] = 1;
                $this->data['newsSource'] = $data_news[0]->news_orginal;
            } else {
                $this->data['isNewsExists'] = 0;
            }

            //show GUI
            $this->load->view('common/common_view_admin', $this->data);
        }
    }
    
    //approve all news from news_auto to news
    function approve_all(){
        list($this->data['newsCount'], $this->data['aNews']) = $this->_get_news_from_db(0, 0);
        $iRow_Count = $this->data['newsCount'];
        $listNewsAuto = $this->data['aNews'];
        $countSuccess = 0;
        $img_save = "upimg_man/image/news/";
        
        for($i=0; $i<$iRow_Count; $i++){
            $newsAutoId = $listNewsAuto[$i]->id;
            $title_temp = explode("&nbsp", $listNewsAuto[$i]->title, 2);
            if(sizeof($title_temp) == 2){
                $title = $title_temp[0];
                $temp1 = explode("(", $title_temp[1], 2);
                $temp2 = explode(")", $temp1[1], 2);
                $date_temp = explode("/", $temp2[0]);
                $date = $date_temp[2] . "-" . $date_temp[1] . "-" . $date_temp[0];
            }
            else{
                $title = $listNewsAuto[$i]->title;
                $date = rand(2013, 2014) . "-" . rand(1, 12) . "-" . rand(1, 28);
            }
            $hour = rand(0, 23);
            $minute = rand(0, 59);
            $date_display = $date . " " . ($hour < 10 ? "0".$hour : $hour) . ":" . ($minute < 10 ? "0".$minute : $minute) . ":00";
            $short_desc = str_replace("(HQ Online)-", "", $listNewsAuto[$i]->desc);
            $full_desc = $listNewsAuto[$i]->content;
            if($listNewsAuto[$i]->img == ""){
                $img = "no-image.jpg";
            }
            else{
                $img = $listNewsAuto[$i]->img;
            }
            $news_source = $listNewsAuto[$i]->news_orginal;
            $strReplaceSource = array("(HQ Online)-", "C&ocirc;ngThương");
            $strReplaceDesc = array("", "");
            
            $this->submitdata['cate_id'] = 1;
            $this->submitdata['parent_cate_url'] = $this->Adminmodel->_get_url_of_parent_cate(1); //get url của cate cha của cate news hiện hành.
            $this->submitdata['title'] = $title;
            $this->submitdata['url'] = convertToVn(trim($title));
            $this->submitdata['short_desc'] = $short_desc;
            $this->submitdata['full_desc'] = str_replace($strReplaceSource, $strReplaceDesc, base64_decode($full_desc)); 
            $this->submitdata['create_user'] = $this->Adminmodel->_get_session(SESSION_ADMINUSERNAME);
            $this->submitdata['create_date'] = date("Y-m-d H:i:s");
            $this->submitdata['online_date'] = date("Y-m-d H:i:s");
            $this->submitdata['tag'] = "";
            $this->submitdata['is_hot'] = 0;
            $this->submitdata['display_date'] = $date_display;
            $this->submitdata['status'] = 1;
            $this->submitdata['img'] = $img_save . $img;
            $this->submitdata['img_thumb'] = $img_save . $img;
            $this->submitdata['img_thumb2'] = $img_save . $img;
            $this->submitdata['img_thumb3'] = $img_save . $img;
            $this->submitdata['source'] = $news_source;
            
            //insert news
            $insert_id = $this->Adminmodel->_insert_news('news', $this->submitdata);
            if($insert_id > 0){
                $this->Adminmodel->_update_status_news_auto($newsAutoId, 1);
                $countSuccess++;
            }
        }
        
        echo $countSuccess;
    }

    //local func : for combobox
    function ddl_get_news_cate_list() {
        $tmp1 = $this->Adminmodel->_get_news_cate_list();
        $tmp2 = $tmp1;
        $b = array();
        foreach ($tmp1 as $key1 => $item1):
            if ($item1->parent_id == "") {
                $b[] = (object) array("parent_id" => $item1->parent_id,
                            "parent_name" => $item1->parent_name,
                            "cate_id" => $item1->cate_id,
                            "cate_name" => "&nbsp;&bull;&nbsp;" . $item1->cate_name,
                            "url" => $item1->url,
                            "status" => $item1->status
                );
                foreach ($tmp2 as $key2 => $item2):
                    if ($item1->cate_id == $item2->parent_id) {
                        $b[] = (object) array("parent_id" => $item2->parent_id,
                                    "parent_name" => $item2->parent_name,
                                    "cate_id" => $item2->cate_id,
                                    "cate_name" => "&nbsp;&bull;&nbsp;&bull;&nbsp;&bull;&nbsp;" . $item2->cate_name,
                                    "url" => $item2->url,
                                    "status" => $item2->status
                        );
                    }
                endforeach;
            }
        endforeach;
        return $b;
    }

    function delete_news_auto() {
        $newsId = $_POST['newsId'];
        if ($newsId != "") {
            $result = $this->Adminmodel->_delete_news_auto($newsId);
            echo $result;
        } else {
            echo 0;
        }
    }
    
}

?>
